package com.cloudkinto.controller.product;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.product.ProductInfoService;
import com.cloudkinto.service.product.vo.MaterialRes;
import com.cloudkinto.service.product.vo.ProductAddReq;
import com.cloudkinto.service.product.vo.ProductUpdateReq;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-04
 */
@RestController
@Api(tags = "商品模块")
@RequestMapping("/api/product")
public class ProductInfoController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ProductInfoController.class);

    @Autowired
    private ProductInfoService service;


    @UserSessionRequired
    @GetMapping("/getMaterialList")
    @ApiOperation(value = "耗材列表", notes = "耗材列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "length", value = "长"),
            @ApiImplicitParam(name = "width", value = "宽"),
            @ApiImplicitParam(name = "height", value = "高"),
            @ApiImplicitParam(name = "storageId", value = "仓库"),
            @ApiImplicitParam(name = "commonUse", value = "耗材类型 0 通用 1自费"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = MaterialRes.class))
    public PageResult getMaterialList(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String companyC = request.getParameter("companyId");
        if (StringUtils.isBlank(companyC)) {
            Long companyId = getCompanyId();
            if (companyId != null) {
                map.put("companyId", companyId + "");
            }
        }
        else {
            map.put("companyId", companyC);
        }
        String length = request.getParameter("length");
        String width = request.getParameter("width");
        String height = request.getParameter("height");
        String storageId = request.getParameter("storageId");
        String commonUse = request.getParameter("commonUse");

        map.put("length", length);
        map.put("width", width);
        map.put("height", height);
        map.put("storageId", storageId);
        map.put("commonUse", commonUse);

        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getMaterialList(map);
    }


    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated ProductAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId, companyId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }


    @UserSessionRequired
    @DeleteMapping("/deleteProductFile/{id}")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult deleteProductFile(@PathVariable("id") Long id) {
        service.deleteProductFile(id);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated ProductUpdateReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("修改成功", service.update(req, userId, companyId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id禁用/启用", notes = "根据id禁用/启用")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("状态修改成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }


    @UserSessionRequired
    @GetMapping("/detailAll/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detailAll(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detailAll(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "productType", value = "产品类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String productType = request.getParameter("productType");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("companyId", String.valueOf(companyId));
        map.put("productType", productType);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/listPageAll")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "productType", value = "产品类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "enable", value = "0查所有 1 仅查启用"),
            @ApiImplicitParam(name = "typeOfProduct", value = "类别 普通产品 common  组合产品 combination"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPageAll(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String productType = request.getParameter("productType");
        String enable = request.getParameter("enable");
        String typeOfProduct = request.getParameter("typeOfProduct");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (StringUtils.isNotBlank(typeOfProduct)) {
            map.put("typeOfProduct", typeOfProduct);
        }
        map.put("companyId", String.valueOf(companyId));
        map.put("productType", productType);
        map.put("enable", enable);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPageAll(map);
    }


    @UserSessionRequired
    @GetMapping("exportProduct")
    @ApiOperation(value = "导出所有数据excel")
    public void exportProduct(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String productType = request.getParameter("productType");
        String enable = request.getParameter("enable");
        String typeOfProduct = request.getParameter("typeOfProduct");

        if (StringUtils.isNotBlank(typeOfProduct)) {
            map.put("typeOfProduct", typeOfProduct);
        }
        map.put("companyId", String.valueOf(companyId));
        map.put("productType", productType);
        map.put("enable", enable);
        map.put("keyword", keyword);

        service.exportProduct(map, response);
    }




    @UserSessionRequired
    @PostMapping("/addProductFile")
    @ApiOperation(value = "批量上传产品信息")
    public SingleResult addProductFile(MultipartFile file) throws FileNotFoundException {
        service.importProductFile(file, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/exportProductPdf")
    @ApiOperation(value = "导出產品标文件", notes = "导出產品标文件")
    public void exportProductPdf(Long id, int count, HttpServletResponse response) throws IOException {
        Long userId = getCurrentUserId();
        service.exportProductCodePdf(id, count, response);
    }



    @UserSessionRequired
    @GetMapping("/printBarCodePdf")
    @ApiOperation(value = "导出產品标文件", notes = "导出產品标文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "count", value = "打印数量", required = true),
            @ApiImplicitParam(name = "length", value = "长",required = true),
            @ApiImplicitParam(name = "width", value = "宽", required = true),
            @ApiImplicitParam(name = "id", value = "产品id", required = true),
            @ApiImplicitParam(name = "commoditySku", value = "产品sku 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "storageSku", value = "仓库sku 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "fnSku", value = "fnSku 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "madeInChina", value = "madeInChina 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "printDate", value = "printDate 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "productName", value = "产品名称 给任何值 都表示选中",required = true),
            @ApiImplicitParam(name = "productSpec", value = "产品规格 给任何值 都表示选中",required = true),})
    public void printBarCodePdf(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String length = request.getParameter("length");
        String width = request.getParameter("width");
        String count = request.getParameter("count");
        String id = request.getParameter("id");

        Map<String, String> map = new HashMap<>();

        String commoditySku = request.getParameter("commoditySku");
        String storageSku = request.getParameter("storageSku");
        String fnSku = request.getParameter("fnSku");
        String madeInChina = request.getParameter("madeInChina");
        String printDate = request.getParameter("printDate");
        String productName = request.getParameter("productName");
        String productSpec = request.getParameter("productSpec");

        if (StringUtils.isNotBlank(commoditySku)) {
            map.put("commoditySku", commoditySku);
        }
        if (StringUtils.isNotBlank(storageSku)) {
            map.put("storageSku", storageSku);
        }
        if (StringUtils.isNotBlank(fnSku)) {
            map.put("fnSku", fnSku);
        }
        if (StringUtils.isNotBlank(madeInChina)) {
            map.put("madeInChina", madeInChina);
        }
        if (StringUtils.isNotBlank(printDate)) {
            map.put("printDate", printDate);
        }
        if (StringUtils.isNotBlank(productName)) {
            map.put("productName", productName);
        }
        if (StringUtils.isNotBlank(productSpec)) {
            map.put("productSpec", productSpec);
        }


        service.printBarCodePdf(map, Long.parseLong(id),Integer.parseInt(length), Integer.parseInt(width), Integer.parseInt(count),response);
    }

}

